<style src="./index.scss" lang="scss" scoped rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
    mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
    props: {
        // 使用者window实例 必定义
            appWindow: {
                type: Object,
                default: null,
            },
    },
    computed: {},

    data() {
        return {

            // 初始化状态
            initLoad: false,
            // 编辑类型
            editType: "",
            // 商品分类数据源
            categoryOptions: [],
            // 商品类型
            dict_goods_type: [],
            form: {
                // 商品标签
                labelIdLst: [],
                // 商品品牌
                brandId: "",
                // 初始销量
                initSales: "",
                // 上架状态：0上架1下架
                publishStatus: 0,
                // 是否显示销量 0否 1是
                isShowSales: 0,
                // 是否显示库存 0否 1是
                isShowStock: 0,
                // 是否开启自动上下架 0否 1是
                publishAuto: 0,
                // 自动上下架时间
                publishTimeStr: "",
                // 库存计算方式
                stockType: 1,
            },
            // 商品标签数据源
            labelOptions: [],
            // 商品品牌数据源
            brandOptions: [],
            // 库存计算方式
            dict_goods_sku: [],
        };
    },

    methods: {
        // 判断是否数字 不可为空 不可小于0 不可有小数
        validateNum(rule, value, callback) {
            if (value !== "") {
                if ((/^[0-9]\d*$/).test(value) == false) {
                    callback(new Error("限正整数"));
                } else {
                    callback();
                }
            } else {
                callback();
            }
        },
        validate(e) {
            return this.mixinEdit_formValidate("editForm");
        },
        getData() {
            let _d = {
                labelIdLst: this.form.labelIdLst,
                brandId: this.form.brandId,
                initSales: this.form.initSales,

                publishStatus: this.form.publishStatus,
                isShowStock: this.form.isShowStock,
                isShowSales: this.form.isShowSales,
                publishAuto: this.form.publishAuto,
                publishTimeStr: this.form.publishTimeStr,
                stockType: this.form.stockType,
            };
            return _d;
        },
        async install(params_) {
            this.editType = params_.type;

            // 数据字典 库存计算方式
            // this.form.stockType = await this.mixinBase_configDict({
            //     type: "goods_sku",
            //     name: "dict_goods_sku",
            // },this.appWindow);
            // 数据源 标签
            this.labelOptions = await this.$vFN.Tools.getSelectListSource({
                url: "mall-site-portal/product/listLabel",
            },this.appWindow);
            // 数据源 品牌
            this.brandOptions = await this.$vFN.Tools.getSelectListSource({
                url: "mall-site-portal/product/listBrand",
            },this.appWindow);
            

            if (params_.datasource) {
                this.form.labelIdLst = params_.datasource.labelLst.map(
                    (item) => {
                        return item.labelId;
                    }
                );
                this.form.brandId = params_.datasource.brandId;
                this.form.initSales = params_.datasource.initSales;
                this.form.publishStatus = params_.datasource.publishStatus;
                this.form.isShowSales = params_.datasource.isShowSales;
                this.form.isShowStock = params_.datasource.isShowStock;
                this.form.publishAuto = params_.datasource.publishAuto;
                this.form.publishTimeStr = params_.datasource.publishTimeStr;
                this.form.stockType = params_.datasource.stockType;
            }
            this.initLoad = true;
            this.returnData();
        },
        async uninstall() {
            this.$refs["editForm"].resetFields();
        },
        returnData() {
            this.$emit("change", {
                data: this.getData(),
                load: this.initLoad,
            });
        },
    },
    mounted() {},
    beforeCreate() {},
    watch: {
        form: {
            handler(newName, oldName) {
                this.returnData();
            },
            immediate: true,
            deep: true,
        },
    },
};
</script>